var jsondata=null;
var userEdit_id;
//用户列表初始化
var grid=$("#userTable").bootgrid({
	ajax: true,
    post: function ()
    {
        return jsondata;
    },
    url: "/platform/windows/user/showpage",
    css: {
        icon: 'zmdi icon',
        iconColumns: 'zmdi-view-module',
        iconDown: 'zmdi-expand-more',
        iconRefresh: 'zmdi-refresh',
        iconUp: 'zmdi-expand-less',
        iconAdd:'zmdi-view-module'
    },
    templates: {
        header: "<div id=\"{{ctx.id}}\" class=\"{{css.header}}\"><div class=\"row\"><div class=\"col-sm-10 actionBar\"><p class=\"{{css.search}}\"></p><p class=\"{{css.actions}}\"></p></div>"
        	+"<div class=\"col-sm-2 dropdown-btn-demo\">"
        	+"<div class=\"btn-group\">"
	        +"<button type=\"button\" class=\"btn btn-default\">管理工具</button>"
	        +"<button type=\"button\" class=\"btn btn-default dropdown-toggle\" data-toggle=\"dropdown\" aria-expanded=\"false\">"
	        +"<span class=\"caret\"></span>"
	        +"<span class=\"sr-only\"></span>"        
	        +"</button>"        
	        +"<ul class=\"dropdown-menu\" role=\"menu\">"    
	        +"<li><a href=\"javascript:gotoAdd();\">用户新增</a></li>"           
	        +"<li class=\"divider\"></li>"        
	        +"<li>"
	        +"<li class=\"hidden-xs\" id=\"chat-trigger\" data-trigger=\"#orgTree\">"
	        +"<a href=\"\">组织机构</a>"
	        +"</li>"
	        +"</li>" 
	        +"</ul></div>"        
        	+"</div></div></div>"
    },
    labels: {
        all: "全部",
        infos: "当前显示 {{ctx.start}} 到 {{ctx.end}} 条，共 {{ctx.total}} 条记录",
        loading: "数据加载中。",
        noResults: "对不起，查询不到任何相关数据",
        refresh: "刷新",
        search: "查询"
    },
    formatters: {
    	"enabled": function(column, row)
        {
    		if(row.enabled=="1")
    		{
    			return "已启动";
    		}
    		else
    		{
    			return "已禁用";
    		}
        },
        "commands": function(column, row)
        {
        	return "<button type=\"button\" class=\"btn btn-icon command-edit waves-effect waves-circle\" data-row-id=\"" + row.id + "\"><span class=\"zmdi zmdi-edit\"></span></button> " + 
            "<button type=\"button\" class=\"btn btn-icon command-delete waves-effect waves-circle\" data-row-id=\"" + row.id + "\"><span class=\"zmdi zmdi-delete\"></span></button>";
        }
    }
}).on("loaded.rs.jquery.bootgrid", function()
{
    grid.find(".command-edit").on("click", function(e)
    {
        gotoEdit($(this).data("row-id"));
    }).end().find(".command-delete").on("click", function(e)
    {
        //alert("You pressed delete on row: " + $(this).data("row-id"));
        gotoDelete($(this).data("row-id"));
    });
});

//组织机构树形菜单的控制方法
function makeSelected(id)
{
	//关闭树形菜单
	closeTree();
	jsondata={
		"orgid":id
	};
	//重新加载列表
	$("#userTable").bootgrid("reload");
}

//跳转到编辑页面
function gotoEdit(id)
{
	userEdit_id=id;
	window.location.href = getWebRootPath()+"/windows/userEdit.html";
}

//跳转到新增页面
function gotoAdd(){
	window.location.href=getWebRootPath()+"/windows/userAdd.html"
}

//删除
function gotoDelete(id){
	if(confirm("确定删除这条记录吗？")){
	$.ajax({
		type: "POST",
        url:"/platform/windows/user/delete",
        data:{"userId":id},
        async: false,
        success: function(data) {
        	if(data.message=="ok"){
        		alert("删除成功!");
        		location.reload();
        	}else{
        		alert("删除失败!");
        		location.reload();
        	}
        	
        }	
	});
		
	}
	
}

